<?php
/*
 * Created on 07-06-2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class CyclicReservationsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	public $name = 'CyclicReservations';

/**
 * Default helper
 *
 * @var array
 */
	public $helpers = array();

/**
 * @var array
 */
	public $uses = array('Court', 'User', 'Reservation', 'CyclicReservation');
	
	public function isAuthorized($user) {
		if (in_array($user['role'], array('admin', 'superAdmin'))) {
			return true;
		}
		if(in_array($this->action, array('delete'))) {
			$reservation = $this->Reservation->find('first', array('conditions'=> array('Reservation.cyclic_reservation_id'=>$this->request->params['pass'][0])));
			if($user['id'] != $reservation['Reservation']['user_id']){
				return false;
			}
			return true;
		}
		if(in_array($this->action, array('add', 'conflicts', 'save', 'cancel', 'jsSetAvailableHours'))) {
			return true;
		}
		return false;
	}
	
	public function add() {
		$this->set('title_for_layout', 'Rezerwacje - dodaj rezerwację');
		$user = $this->Auth->user();
		if($this->request->is('post')) {
			$data = $this->request->data;
			$cyclic['CyclicReservation'] = array(
				'start_date' => $data['CyclicReservation']['start_time'],
				'end_date' => $data['CyclicReservation']['end_time'],
				'how_often' => $data['CyclicReservation']['how_often']
			);
			$id = $this->CyclicReservation->save($cyclic);
			$id = $id['CyclicReservation']['id'];
			$data['CyclicReservation']['user_id'] = $user['id'];
			$data['CyclicReservation']['cyclic_reservation_id'] = $id;
			unset($data['CyclicReservation']['end_time']);
			unset($data['CyclicReservation']['how_often']);
			$reservations = array();
			$conflicts = array();
			while($data['CyclicReservation']['start_time'] < date("Y-m-d H:i:s", strtotime($cyclic['CyclicReservation']['end_date']. " +1 day"))) {
				if (!$this->Reservation->find('first', array('conditions'=>array(
																			'Reservation.court_id'=>$data['CyclicReservation']['court_id'], 
																			'Reservation.start_time'=>$data['CyclicReservation']['start_time'])))) {
					array_push($reservations, $data['CyclicReservation']);																
				} else {
					array_push($conflicts, $data['CyclicReservation']);
				}
				$data['CyclicReservation']['start_time'] = date("Y-m-d H:i:s", strtotime($data['CyclicReservation']['start_time']. " +".$cyclic['CyclicReservation']['how_often']."day"));
			}
			unset($data['CyclicReservation']);
			if($conflicts) {
				$this->Session->write('CyclicReservation.reservations', $reservations);
				$this->Session->write('CyclicReservation.conflicts', $conflicts);
				$this->redirect(array('controller'=>'CyclicReservations', 'action'=>'conflicts'));
			} else {
				if($this->Reservation->saveAll($reservations)) {
					$this->Session->setFlash('Rezerwacja została dodana.', 'flash_success');
				} else {
					$this->Session->setFlash('Nie udało się dodać rezerwacji.', 'flash_failure');
				}
				$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
			}
		}
	}
	
	public function delete($id = null) {
		$user = $this->Auth->user();
		if($this->CyclicReservation->delete($id, true)) {
			$this->Session->setFlash('Rezerwacje usunięte.', 'flash_success');
		} else {
			$this->Session->setFlash('Nie udało się usunąć rezerwacji.', 'flash_failure');
		}
		$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));   
    }
	
	public function conflicts() {
		$reservations = $this->Session->read('CyclicReservation.reservations');
		$conflicts = $this->Session->read('CyclicReservation.conflicts');
		$this->set('reservations', $reservations);
		$this->set('conflicts', $conflicts);
	}
	
	public function save() {
		$user = $this->Auth->user();
		$reservations = $this->Session->read('CyclicReservation.reservations');
		if($this->Reservation->saveAll($reservations)) {
			$this->Session->setFlash('Rezerwacja została dodana.', 'flash_success');
		} else {
			$this->Session->setFlash('Nie udało się dodać rezerwacji.', 'flash_failure');
		}
		$this->Session->write('CyclicReservation.reservations', array());
		$this->Session->write('CyclicReservation.conflicts', array());
		$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
	}
	
	public function cancel() {
		$user = $this->Auth->user();
		$this->Session->write('CyclicReservation.reservations', array());
		$this->Session->write('CyclicReservation.conflicts', array());
		$this->redirect(array('controller'=>'Reservations', 'action'=>'view', $user['username']));
	}
}
?>
